/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frameManejoCategorias.java
 *
 * Created on 13/04/2009, 11:06:34 PM
 */

package gestiontipocampo;

/**
 *
 * @author Ricardo
 */
import java.util.Vector;
import javax.swing.JOptionPane;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.lang.Integer;



public class frameManejoCategorias extends javax.swing.JFrame {

    /** Creates new form frameManejoCategorias */
    public frameManejoCategorias(String nombre) {
        initComponents();
        labelNombreCategoria.setText(nombre);
        nombreGlobal=nombre;
    }
    public int IDCategoria=0;
        public String nombreGlobal;
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        listInstanciasCategorias = new javax.swing.JList();
        labelNombreCategoria = new javax.swing.JLabel();
        labelElementos = new javax.swing.JLabel();
        botonAgregarElemento = new javax.swing.JButton();
        botonAceptar = new javax.swing.JButton();
        botonCancelar = new javax.swing.JButton();
        botonBorrarElemento = new javax.swing.JButton();
        botonModificarElemento = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        listInstanciasCategorias.setName("listInstanciasCategorias"); // NOI18N
        jScrollPane1.setViewportView(listInstanciasCategorias);

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(gestiontipocampo.GestionTipoCampoApp.class).getContext().getResourceMap(frameManejoCategorias.class);
        labelNombreCategoria.setText(resourceMap.getString("labelNombreCategoria.text")); // NOI18N
        labelNombreCategoria.setName("labelNombreCategoria"); // NOI18N

        labelElementos.setText(resourceMap.getString("labelElementos.text")); // NOI18N
        labelElementos.setName("labelElementos"); // NOI18N

        botonAgregarElemento.setText(resourceMap.getString("botonAgregarElemento.text")); // NOI18N
        botonAgregarElemento.setName("botonAgregarElemento"); // NOI18N
        botonAgregarElemento.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                botonAgregarElementoMouseClicked(evt);
            }
        });
        botonAgregarElemento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonAgregarElementoActionPerformed(evt);
            }
        });

        botonAceptar.setText(resourceMap.getString("botonAceptar.text")); // NOI18N
        botonAceptar.setName("botonAceptar"); // NOI18N
        botonAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonAceptarActionPerformed(evt);
            }
        });

        botonCancelar.setText(resourceMap.getString("botonCancelar.text")); // NOI18N
        botonCancelar.setName("botonCancelar"); // NOI18N
        botonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonCancelarActionPerformed(evt);
            }
        });

        botonBorrarElemento.setText(resourceMap.getString("botonBorrarElemento.text")); // NOI18N
        botonBorrarElemento.setName("botonBorrarElemento"); // NOI18N
        botonBorrarElemento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonBorrarElementoActionPerformed(evt);
            }
        });

        botonModificarElemento.setText(resourceMap.getString("botonModificarElemento.text")); // NOI18N
        botonModificarElemento.setName("botonModificarElemento"); // NOI18N
        botonModificarElemento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonModificarElementoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(labelElementos))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(102, 102, 102)
                        .addComponent(labelNombreCategoria, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(103, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(119, Short.MAX_VALUE)
                .addComponent(botonAceptar)
                .addGap(18, 18, 18)
                .addComponent(botonCancelar)
                .addGap(117, 117, 117))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 156, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(botonBorrarElemento, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(botonAgregarElemento, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(botonModificarElemento))
                .addGap(18, 18, 18))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelNombreCategoria)
                .addGap(15, 15, 15)
                .addComponent(labelElementos)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(botonAgregarElemento)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(botonModificarElemento)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(botonBorrarElemento)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 35, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botonAceptar)
                    .addComponent(botonCancelar))
                .addGap(20, 20, 20))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botonAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonAceptarActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_botonAceptarActionPerformed

    private void botonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonCancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_botonCancelarActionPerformed

    private void botonAgregarElementoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonAgregarElementoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_botonAgregarElementoActionPerformed

    private void botonModificarElementoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonModificarElementoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_botonModificarElementoActionPerformed

    private void botonBorrarElementoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonBorrarElementoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_botonBorrarElementoActionPerformed

    private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
        // TODO add your handling code here:
        ControladorBD buscador= new ControladorBD();
        buscador.doUpdate("INSERT INTO TIPOCATEGORIA (nombre, descripcion) VALUES ('"+labelNombreCategoria.getText()+"','No establecida')");
        refrescarLista();

    }//GEN-LAST:event_formComponentShown

    private void botonAgregarElementoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_botonAgregarElementoMouseClicked

        String elemento = JOptionPane.showInputDialog(null, "Digite el elemento a agregar", "Nuevo elemento", JOptionPane.QUESTION_MESSAGE);// TODO add your handling code here:
        ControladorBD buscador= new ControladorBD();
        //buscador.doUpdate("INSERT INTO TIPOCATEGORIA (nombre, descripcion) VALUES ('"+labelNombreCategoria.getText()+"','No establecida')");
        //refrescarLista();
        
          //     ControladorBD buscador = new ControladorBD();



            try {
                //ResultSet resultado = buscador.getResultSet("SELECT  ID FROM TIPOCATEGORIA order by ID desc limit 1");
                ResultSet resultado = buscador.getResultSet("SELECT  ID FROM TIPOCATEGORIA where nombre='"+nombreGlobal+"'");
                int contador=0;
                while (resultado.next()) {
                    //vectorValores.add(new MiDato(resultado.getObject("nombre").toString(),Integer.parseInt(resultado.getObject("ID").toString())));
                    System.out.println(resultado.getObject("ID").toString());
                    this.IDCategoria=Integer.parseInt(resultado.getObject("ID").toString());
                    contador++;
                }
            } catch (SQLException e) {
                System.out.println("*SQL Exception: *" + e.toString());
            }

        buscador.doUpdate("INSERT INTO INSTANCIACATEGORIA (IDTipoCategoria, valor) VALUES ('"+this.IDCategoria+"','"+elemento.toString()+"')");
        refrescarLista();




    }//GEN-LAST:event_botonAgregarElementoMouseClicked




    public void refrescarLista(){

/*Vector listaInstancias = new Vector();
listaInstancias.add("uno");
listaInstancias.add("dos");
listaInstancias.add("tres");*/

        Modelo  miModelo = new Modelo();
        listInstanciasCategorias.setListData(miModelo.getModeloDeCombo("Select ic.ID , ic.valor as nombre from TIPOCATEGORIA tc, INSTANCIACATEGORIA ic where ic.IDTIpoCategoria=tc.ID and tc.nombre like '"+labelNombreCategoria.getText().trim()+"'"));


       // listInstanciasCategorias.setListData(listaInstancias);

    }
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new frameManejoCategorias("").setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonAceptar;
    private javax.swing.JButton botonAgregarElemento;
    private javax.swing.JButton botonBorrarElemento;
    private javax.swing.JButton botonCancelar;
    private javax.swing.JButton botonModificarElemento;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel labelElementos;
    private javax.swing.JLabel labelNombreCategoria;
    private javax.swing.JList listInstanciasCategorias;
    // End of variables declaration//GEN-END:variables

}
